<!DOCTYPE html>
<html lang="zh-cmn-Hans" prefix="og: http://ogp.me/ns#" class="han-init">
<head>
  <meta charset="utf-8">
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <link rel="icon" href="http://upeng.github.io/favicon.ico">
  <title>upeng</title>
  <link rel="stylesheet" href="/css/style.css">
  <link rel="stylesheet" href="/lib/fancybox/jquery.fancybox-1.3.4.css">
  <!--在这里倒入jquery 方便处理部分页面的jquery-->
  <script src="https://cdn.staticfile.org/jquery/1.7/jquery.min.js" type="text/javascript" ></script>
</head>

<body>
	<header class="site-header navfixed-false">
  <div class="container">
      <h1><a href="/" title="upeng"><span class="octicon octicon-mark-github"></span> upeng</a></h1>
      <nav class="site-header-nav" role="navigation">
        
              
              <a href="/"  class=" site-header-nav-item hvr-underline-from-center" title="Home">Home</a>
        
              
              <a href="/categories/"  class=" site-header-nav-item hvr-underline-from-center" title="Categories">Categories</a>
        
              
              <a href="/bookmark/"  class=" site-header-nav-item hvr-underline-from-center" title="Bookmark">Bookmark</a>
        
              
              <a href="http://shareup.sinaapp.com"  class=" site-header-nav-item hvr-underline-from-center" title="Share">Share</a>
        
              
              <a href="/about/"  class=" site-header-nav-item hvr-underline-from-center" title="About">About</a>
        
      </nav>
  </div>
</header>

	
<section class="collection-head geopattern" data-pattern-id="几个值得深思的 PHP 面试问题" >
    <div class="container">
        <div class="collection-title">
            <h1 class="collection-header">
                几个值得深思的 PHP 面试问题
            </h1>
            
                <div class="collection-info">
                    <span class="meta-info">
                        <span class="octicon octicon-calendar"></span>
                        <time datetime="2016-05-27T16:04:26.000Z" itemprop="datePublished">2016-05-28</time>
                    </span>
                    
                        <span class="meta-info">
                            <span class="octicon octicon-file-directory"></span>
                            <a href='/categories/PHP/' title=''>PHP</a>
                        </span>
                    
                </div>
            
        </div>
    </div>
</section>
	<section class="container">
    <div class="columns">
        <!-- -->
        <div class="column three-fourths">
            <article class="article-content markdown-body">
                <p>5个值得深思的 PHP 面试问题<br>文章所罗列的问题虽然看似简单，但是每个背后都涵盖了一个或几个大家容易忽视的基础知识点，希望能够帮助到你的面试和平时工作。<br><a id="more"></a><br><strong>Q1</strong></p>
<pre><code>$str1 = &#39;hello world&#39;;
$str2 = &#39;he&#39;;
if (strpos($str1, $str2))
{
    echo &quot;/&quot;&quot; . $str1 . &quot;/&quot; contains /&quot;&quot; . $str2 . &quot;/&quot;&quot;;
}
else
{
    echo &quot;/&quot;&quot; . $str1 . &quot;/&quot; does not contain /&quot;&quot; . $str2 . &quot;/&quot;&quot;;
}
</code></pre><p>正确运行的输出结果:<br><!-- more --><br><code>&quot;hello world&quot; does not contain &quot;he&quot;</code><br>strpos是返回字符串str2在str1的位置，没有找到则返回false；php是弱类型语言；然而实际上这次返回了0而在if语句中0也被当作false,所以我们需要对false做类型判断，正确的代码如下:</p>
<pre><code>$str1 = &#39;hello world&#39;;
$str2 = &#39;he&#39;;
if (strpos($str1, $str2) !== false)
{
    echo &quot;/&quot;&quot; . $str1 . &quot;/&quot; contains /&quot;&quot; . $str2 . &quot;/&quot;&quot;;
}
else
{
    echo &quot;/&quot;&quot; . $str1 . &quot;/&quot; does not contain /&quot;&quot; . $str2 . &quot;/&quot;&quot;;
}
</code></pre><p>需要注意的是我们使用了!==，在php 和 JS中= !相对== 更为严格需要要求数据类型一致。</p>
<p><strong>Q2</strong><br>下面的输出结果会是怎样？</p>
<pre><code>$x = 5;
echo $x;
echo &quot;&lt;br /&gt;&quot;;
echo $x+++$x++;
echo &quot;&lt;br /&gt;&quot;;
echo $x;
echo &quot;&lt;br /&gt;&quot;;
echo $x---$x--;
echo &quot;&lt;br /&gt;&quot;;
echo $x;
</code></pre><p>实际运行结果是</p>
<pre><code>5
11
7
1
5
</code></pre><p>关于 $x++ 和 $x–这个问题其实非常容易遇见，我们只需记住$x++使用最近的值，然后才自增。<br>运算符的优先级，++ 是明显高于 +，因此先执行++ 再执行 + 。关于运算符的优先级，有的时候我们真的可以通过括号来让我们的程序更让人直观的了解，毕竟代码不光是用于执行的，有的时候或许团队的可读性也是提高效率的一种。<br><strong>Q3</strong><br>关于变量的引用；</p>
<pre><code>$a = &#39;1&#39;;
$b = &amp;$a;
$b = &quot;2$b&quot;;
</code></pre><p>请问 $a 和 $b的值各位多少<br>部分第一时间会想到 $a=’1’  $b=’21’,仔细一看 $b=&amp;$a,这里$b是变量$a的引用而不是直接 赋值。</p>
<p><strong>Q4</strong><br>下面是true还是false</p>
<pre><code>var_dump(0123 == 123)；
var_dump(&#39;0123&#39; == 123);
var_dump(&#39;0123&#39; == 123);
</code></pre><p>var_dump(0123 == 123);// false,PHP会默认把0123当作8进制来处理，实际转化为10进制就是83，显然这不是相等的。<br>var_dump(’0123′ == 123);// true这里php会非常有趣的将’0123′转换成一个数字而且默认去掉了前面的0也就是123==123<br>var_dump(’0123′ === 123);// false很显然上面的问题已经说过了数字和字符串类型不一致。</p>
<p><strong>Q5</strong><br>下面的代码有什么问题吗？输出会是什么，怎样修复它</p>
<pre><code>$referenceTable = array();
referenceTable[&#39;val1&#39;] = array(1, 2);
referenceTable[&#39;val2&#39;] = 3;
referenceTable[&#39;val3&#39;] = array(4, 5);

$testArray = array();

$testArray = array_merge($testArray, $referenceTable[&#39;val1&#39;]);
var_dump($testArray);
$testArray = array_merge($testArray, $referenceTable[&#39;val2&#39;]);
var_dump($testArray);
$testArray = array_merge($testArray, $referenceTable[&#39;val3&#39;]);
var_dump($testArray);
</code></pre><p>实际输出如下：</p>
<pre><code>array(2) {[0] =&gt; int(1) [1] =&gt; int(2)}
NULL
NULL
</code></pre><p>运行的时候你或许还能看到下面的警告</p>
<pre><code>warning: array_merge() : Argument #2 is not an array
warning: array_merge() : Argument #1 is not an array
</code></pre><p>array_merge需要传入的参数都是数组，如果不是，则会返回null。 你可以这样修改</p>
<pre><code>$testArray = array_merge($testArray, (array)$referenceTable[&#39;val1&#39;]);
var_dump($testArray);
$testArray = array_merge($testArray, (array)$referenceTable[&#39;val2&#39;]);
var_dump($testArray);
$testArray = array_merge($testArray, (array)$referenceTable[&#39;val3&#39;]);
var_dump($testArray);
</code></pre>
            </article>
            
                <div class="share">
                    <!--开启分享-->
<div class="share-component" data-disabled="google,twitter,facebook" data-description="5个值得深思的 PHP 面试问题文章所罗列的问题虽然看..."></div>

<script src="/js/share.min.js"></script>

                </div>    
            
            
                
<div class="comments">
	<div class="ds-thread" data-thread-key="php-detail" data-title="几个值得深思的 PHP 面试问题" data-url="http://upeng.github.io/2016/05/28/php-detail/"></div>
	<script type="text/javascript">
	var duoshuoQuery = {short_name:"upeng"};
		(function() {
			var ds = document.createElement('script');
			ds.type = 'text/javascript';ds.async = true;
			ds.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + '//static.duoshuo.com/embed.js';
			ds.charset = 'UTF-8';
			(document.getElementsByTagName('head')[0]
			 || document.getElementsByTagName('body')[0]).appendChild(ds);
		})();
	</script>
</div>

            
        </div>
        <div class="column one-fourth">
            
                
                


<h3>Post Directory</h3>

<div id="post-directory-module">
	<section class="post-directory">
		<p><strong class="toc-title">文章目录</strong></p>
		
	</section>
</div>
            
        </div>
    </div>
</section>

<footer class="container">
    <div class="site-footer" role="contentinfo">
        <div class="copyright left mobile-block">
                © 2016
                <span title="yupeng">yupeng</span>
                <a href="javascript:window.scrollTo(0,0)" class="right mobile-visible">TOP</a>
        </div>

        <ul class="site-footer-links right mobile-hidden">
            <li>
                <a href="javascript:window.scrollTo(0,0)" >TOP</a>
            </li>
        </ul>

        <a href="https://github.com/upeng" target="_blank" aria-label="view source code">
            <span class="mega-octicon octicon-mark-github" title="GitHub"></span>
        </a>

        <ul class="site-footer-links mobile-hidden">
            
                  
                  <li>
                    <a href="/"  title="Home">Home</a>
                  </li>
            
                  
                  <li>
                    <a href="/categories/"  title="Categories">Categories</a>
                  </li>
            
                  
                  <li>
                    <a href="/bookmark/"  title="Bookmark">Bookmark</a>
                  </li>
            
                  
                  <li>
                    <a href="http://shareup.sinaapp.com"  title="Share">Share</a>
                  </li>
            
                  
                  <li>
                    <a href="/about/"  title="About">About</a>
                  </li>
            
            <li>
                <a href="/atom.xml">
                    <span class="octicon octicon-rss" style="color:orange;"></span>
                </a>
            </li>
        </ul>
    </div>
</footer>


		<script src="/js/geopattern.js"></script>

		
			<script src="/js/toc.js"></script>
		

		<script src="/js/highlight.pack.js"></script>
		<script src="/lib/fancybox/jquery.fancybox-1.3.4.pack.js"></script>

		<script src="/js/index.js"></script>

		 <script src="/js/popular_repo.js"></script> 

	</body>
</html>